/*----------------------------------------------------------------------------
 * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#include "errno.h"
#include "stdio.h"
#include "stdlib.h"

#include "linux/mtd/mtd.h"

#include "fs/fs.h"

#include "spinor.h"

#include "mtd_common.h"
#include "spinor_common.h"
#include "spinor_scan.h"

#include <linux/module.h>
#include "host_common.h"
#include "los_base.h"
#include "linux/device.h"
#include "linux/platform_device.h"
#include "linux/mtd/mtd_list.h"

/*---------------------------------------------------------------------------*/
struct mtd_info *spinor_mtd = NULL;
static struct spinor_info *spinor = NULL;
static struct spinor_host *host = NULL;
extern int spinor_host_init(struct spinor_host *host);
extern int spinor_node_register(struct mtd_info *mtd);

/*---------------------------------------------------------------------------*/
/* spinor_init - spinor initializtion total entry */
/*---------------------------------------------------------------------------*/
static int hisfc350_probe(struct platform_device *dev)
{
    struct resource *res = NULL;
    spinor_mtd = (struct mtd_info *)zalloc(sizeof(struct mtd_info));
    if (!spinor_mtd) {
        ERR_MSG("no mem for mtd_info!\n");
        return -1;
    }
    spinor = (struct spinor_info *)zalloc(sizeof(struct spinor_info));
    if (!spinor) {
        ERR_MSG("no mem for spinor_info!\n");
        goto err;
    }
    spinor_mtd->priv = spinor;
    host = (struct spinor_host *)zalloc(sizeof(struct spinor_host));
    if (!host) {
        ERR_MSG("no mem for spinor_host!\n");
        goto err1;
    }
    host->spinor = spinor;
    res = platform_get_resource(dev, IORESOURCE_MEM, 0);
    if (!res) {
        goto err2;
    }
    host->regbase = platform_ioremap_resource(res);
    res = platform_get_resource(dev, IORESOURCE_MEM, 1);
    if (!res) {
        goto err2;
    }
    host->membase = platform_ioremap_resource(res);
    spinor->priv = host;
    /* spinor host init */
    if (spinor_host_init(host)) {
        ERR_MSG("spinor host init fail!\n");
        goto err2;
    }

    /* scan spinor device entry */
    if (spinor_scan(spinor_mtd)) {
        ERR_MSG("spinor scan fail!\n");
        goto err2;
    }

    /* spinor register */
    spinor_register(spinor_mtd);
    add_mtd_list("spinor", spinor_mtd);
    return 0;

err2:
    free(host);
    host = NULL;
err1:
    free(spinor);
    spinor = NULL;
err:
    free(spinor_mtd);
    spinor_mtd = NULL;
    return -1;
}

static int hisfc350_suspend(struct device *dev)
{

    dprintf("suspend hisfc350\n");
    return 0;
}

int hisfc350_wakeup_lockresume(void)
{
    if (LOS_OK != LOS_MuxPost(host->lock)) {
        return -1;
    }
    return 0;
}

static int hisfc350_resume(struct device *dev)
{
    int ret = 0;
    if (spinor && spinor->resume) {
        ret = spinor->resume(spinor);
    } else {
        ERR_MSG("spinor or spinor->resume is null!\n");
    }
    return ret;
}
static const struct dev_pm_op hisfc350_dev_pm_ops = {
    .suspend = hisfc350_suspend,
    .resume = hisfc350_resume,
};
static int hisfc350_remove(struct platform_device *dev)
{
    if (del_mtd_list(spinor_mtd)) {
        ERR_MSG("remove spinor fail!\n");
        return -1;
    } else {
        free(host);
        host = NULL;
        free(spinor);
        spinor = NULL;
        free(spinor_mtd);
        spinor_mtd = NULL;
    }
    return 0;
}
static struct platform_driver hisfc350_driver = {
    .probe      = hisfc350_probe,
    .remove     = hisfc350_remove,
    .driver     = {
        .name   = "hisfc350",
        .pm = &hisfc350_dev_pm_ops,
    },
};
int /*__init*/ hisfc350_init(void)
{
    return platform_driver_register(&hisfc350_driver);
}

void /*__exit*/ hisfc350_exit(void)
{
    platform_driver_unregister(&hisfc350_driver);
}
module_init(hisfc350_init);
module_exit(hisfc350_exit);
